// 只在客户端环境中运行mock
if (typeof window !== 'undefined') {
  console.log('开始初始化Mock数据...');
  
  // 模拟数据
  const mockData = {
    users: [
      {
        id: 1,
        name: '张三',
        email: 'zhangsan@example.com',
        avatar: 'https://randomuser.me/api/portraits/men/32.jpg',
      },
      {
        id: 2,
        name: '李四',
        email: 'lisi@example.com',
        avatar: 'https://randomuser.me/api/portraits/women/44.jpg',
      },
    ],
    
    trades: [
      { id: 1, symbol: 'BTC/USDT', price: 45000.50, quantity: 0.5, side: 'buy', status: 'filled', created_at: '2024-01-01 10:00:00' },
      { id: 2, symbol: 'ETH/USDT', price: 3000.75, quantity: 2.0, side: 'sell', status: 'filled', created_at: '2024-01-01 11:30:00' },
      { id: 3, symbol: 'SOL/USDT', price: 100.25, quantity: 10.0, side: 'buy', status: 'partial', created_at: '2024-01-01 12:15:00' },
      { id: 4, symbol: 'BNB/USDT', price: 350.80, quantity: 1.5, side: 'buy', status: 'pending', created_at: '2024-01-01 13:45:00' },
      { id: 5, symbol: 'BTC/USDT', price: 45500.25, quantity: 0.3, side: 'sell', status: 'filled', created_at: '2024-01-01 14:20:00' },
    ],
    
    markets: [
      { symbol: 'BTC/USDT', price: 45200.30, change_24h: 2.5, volume_24h: 12345678.90 },
      { symbol: 'ETH/USDT', price: 3050.45, change_24h: -1.2, volume_24h: 9876543.21 },
      { symbol: 'SOL/USDT', price: 102.75, change_24h: 5.8, volume_24h: 5678901.23 },
      { symbol: 'BNB/USDT', price: 352.10, change_24h: 0.8, volume_24h: 3456789.45 },
    ],
  };
  
  // 保存原始fetch方法
  const originalFetch = window.fetch;
  
  // 重写fetch方法
  window.fetch = async (url, options) => {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, Math.random() * 400 + 200));
    
    // 转换为字符串以方便匹配
    const urlStr = typeof url === 'string' ? url : url.toString();
    
    // 处理API请求
    if (urlStr.includes('/api/users') && (!options || options.method === 'GET')) {
      console.log('Mock拦截了 /api/users 请求');
      return new Response(
        JSON.stringify({ code: 200, data: mockData.users, message: 'success' }),
        { headers: { 'Content-Type': 'application/json' } }
      );
    }
    
    if (urlStr.includes('/api/trades') && (!options || options.method === 'GET')) {
      console.log('Mock拦截了 /api/trades 请求');
      const urlObj = new URL(urlStr);
      const page = parseInt(urlObj.searchParams.get('page') || '1');
      const limit = parseInt(urlObj.searchParams.get('limit') || '10');
      
      const start = (page - 1) * limit;
      const end = start + limit;
      const paginatedTrades = mockData.trades.slice(start, end);
      
      return new Response(
        JSON.stringify({
          code: 200,
          data: {
            list: paginatedTrades,
            total: mockData.trades.length,
            page,
            limit
          },
          message: 'success'
        }),
        { headers: { 'Content-Type': 'application/json' } }
      );
    }
    
    if (urlStr.includes('/api/markets') && (!options || options.method === 'GET')) {
      console.log('Mock拦截了 /api/markets 请求');
      return new Response(
        JSON.stringify({ code: 200, data: mockData.markets, message: 'success' }),
        { headers: { 'Content-Type': 'application/json' } }
      );
    }
    
    if (urlStr.includes('/api/trades') && options && options.method === 'POST') {
      console.log('Mock拦截了 /api/trades POST请求');
      const body = options.body && typeof options.body === 'string' ? JSON.parse(options.body) : {};
      return new Response(
        JSON.stringify({
          code: 200,
          data: {
            id: Date.now(),
            ...body,
            status: 'pending',
            created_at: new Date().toISOString()
          },
          message: '交易创建成功'
        }),
        { headers: { 'Content-Type': 'application/json' } }
      );
    }
    
    // 其他请求使用原始fetch
    return originalFetch(url, options);
  };
  
  console.log('Mock数据已成功初始化，API拦截已启用');
}

// 服务器端导出空对象
export default {};